<?php

/**
 * @author Nguyen Xuan Hong
 * @copyright 2010
 */

class clsTemplate
{
	private $templateFolder = 'template/';
	private $templateExtend = '.html';
	private $templateName ;
	# Store templates block content
	private $templatesBlock = array();
	private $templatesComponen = array();
	public $hasError = array();
	public $templateContent ;
	
	public function clsTemplate($fileName = '', $autogetblock = false){
		if($fileName) {
			# Asign information
			$this->templateName = $fileName;
			# Get template content
			if($this->getTemplateContent() && $autogetblock) {
				# Get templates block			
				$this->getTemplatesBlock();
			}
		}
	}
	
	
	# Get templates
	# Read templates file url
	# Return templates content
	private function getTemplateContent(){
		$strTemplatesFName = $this->templateName;
		if(!preg_match('/^[a-zA-Z0-9]+$/',$strTemplatesFName)) {
			$this->hasError['str'] = "Lỗi tên templates";
			return false ;
		}
		$fileName = $this->templateFolder.$strTemplatesFName.$this->templateExtend ;
		$i = 0 ;
		for ($i = 0; $i<3; $i++) {
			if( @file_exists($fileName) )	{
				$CF = @fopen ($fileName, "r");
				$get = @fread ($CF,@filesize ($fileName));
				@fclose ($CF);
				$this->templateContent = $get ;
				return true;
			}
		}
		$this->hasError['str'] = "Không đọc được template";
		return false ;
	}
	
	public function getTemplatesBlock(){
		$str = $this->templateContent;
		preg_match_all('#<!-- \#BBLOCK (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- \#EBLOCK (.*?) -->#s', $str, $arr, PREG_PATTERN_ORDER);
		$a = array();
		for ($i=0; $i<count($arr[0]); $i++) {
			$a[$arr[1][$i]] = $arr[0][$i];
		}
		$this->templatesBlock = $a;
		preg_match_all('#<!-- BOC (.*?)\((.*?)\) -->(.*?)<!-- EOC (.*?) -->#se',$str,$arr, PREG_PATTERN_ORDER);	//EOC
		$a = array();
		for ($i=0; $i<count($arr[0]); $i++) {
			$a[$arr[1][$i]] = $arr[0][$i];
		}
		$this->templatesComponen = $a;
	}
	
	
	# Get block content
	public function getBlock($blockName){
		//return $this->templatesBlock[$blockName];
		$str = $this->templateContent;
		preg_match_all('#<!-- \#BBLOCK '.$blockName.' -->[\r\n]*(.*?)[\r\n]*<!-- \#EBLOCK '.$blockName.' -->#s', $str, $arr, PREG_PATTERN_ORDER);
		return $arr[1][0];
	}
	
	public function getComp($compName){
		return $this->templatesComponen[$compName];
	}
	
	public static function replaceBlock($blockName,$stringContent,$code){
		return preg_replace('#<!-- \#BBLOCK '.$blockName.' -->[\r\n]*(.*?)[\r\n]*<!-- \#EBLOCK '.$blockName.' -->#s',$stringContent, $code);
	}
	
	# Auto get function templates
	public function parseComponent($func,$exp = '') {
		$exp = trim(stripslashes($exp));
		if ($exp) $code = eval("return ".$func."(".$exp.");");
		else $code = eval("return ".$func."();"); 
		return $code;
	}
	
	
	# Replace element function
	public static function replace($source,$inp = array()){
		foreach($inp as $key => $value ) {
			//$source = str_replace('{'.$key.'}',$value,$source);
			$source = str_replace($key,$value,$source);
		}
		return $source;
	}
	
	# Auto parse templates
	public function parseTeplates() {
		$code = $this->templateContent;
		if(!$code) return '';
		$code = preg_replace('#<!-- BOC (.*?)\((.*?)\) -->(.*?)<!-- EOC (.*?) -->#se', '$this->parseComponent("\\1","\\2","\\3");', $code);	//EOC
		/*$code = str_replace('{TPL_LINK}', "templates/".$_SESSION['current_tpl'], $code);
		$code = str_replace('{WEB_TITLE}', $webTitle, $code);
		$code = preg_replace('#<!-- BEGIN (.*?) -->[\r\n]*(.*?)[\r\n]*<!-- END (.*?) -->#s', '\\2', $code);*/
		return $code;
	}
	
}


?>